﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MusicExplorer.Model
{
    public sealed class Track
    {
        private readonly Dictionary<ArtistRole, List<Artist>> m_artists;

        private readonly string m_id;
        private readonly Repository m_repository;
        private readonly TrackType m_type;
        
        public Track(string id, Repository repository, TrackType type)
        {
            m_artists = new Dictionary<ArtistRole, List<Artist>>();
            m_id = id;
            m_repository = repository;
            m_type = type;
        }

        public string Id { get { return m_id; } }
        public Repository Repository { get { return m_repository; } }
        public TrackType Type { get { return m_type; } }
        public int Order { get; set; }

        public string Name { get; set; }
        public string Url { get; set; }
        public string Mp3 { get; set; }
        public string Artwork { get; set; }
        public DateTime PublishDate { get; set; }
        public int Position { get; set; }

        public string Keywords
        {
            get
            {
                var keyworkds = string.Empty;
                if (m_artists.ContainsKey(ArtistRole.Composer))
                {
                    keyworkds += string.Join(" ", m_artists[ArtistRole.Composer].Select(a => a.Name.ToLower()));
                }

                if (!string.IsNullOrEmpty(keyworkds)) keyworkds += " ";
                keyworkds += Name.ToLower();

                return keyworkds;
            }
        }

        public string YoutubeUrl { get; set; }
        public string SoundcloudStream { get; set; }

        public bool IsRead { get; set; }

        #region Artists
        public Dictionary<ArtistRole, List<Artist>> Artists { get { return m_artists; } }
        public List<Artist> this[ArtistRole role]
        {
            get
            {
                if (m_artists.ContainsKey(role))
                {
                    return m_artists[role];
                }
                else
                {
                    return null;
                }
            }
        }
        #endregion
    }
}
